//
//  UPhotoListView.m
//  ChatForHot
//
//  Created by wr on 2018/10/15.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "UPhotoListView.h"
#import "PhotoListCellTableViewCell.h"

@interface UPhotoListView ()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation UPhotoListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    [self addSubview:self.HOTCHATtopview];
    [self addSubview:self.HOTCHATroundview];
    [self.HOTCHATroundview addSubview:self.HOTCHATphotoListTableview];
    [self addSubview:self.HOTCHATbottomview];
    
    [self.HOTCHATphotoListTableview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.HOTCHATroundview);
    }];
    [self.HOTCHATtopview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self.HOTCHATbottomview.mas_top);
    }];
    [self.HOTCHATbottomview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    [self.HOTCHATroundview mas_updateConstraints:^(MASConstraintMaker *make) {
        //            make.height.mas_equalTo(50);
        //            make.left.right.equalTo(self);
        //            make.bottom.equalTo(self.HOTCHATphotoListTableview.mas_top);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.HOTCHATbottomview.mas_top);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PhotoListCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PhotoListCellTableViewCell"];
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATgetPhotos:)]) {
        [self.delegate HOTCHATgetPhotos:[self.dataArray objectAtIndex:indexPath.row]];
    }
    [self HOTCHATcloseListView];
}

#pragma mark - Action
- (void)HOTCHATshowInView:(UIView *)sview array:(NSArray *)array{
    [sview addSubview:self];
    self.dataArray = array;
    [self.HOTCHATphotoListTableview reloadData];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(sview);
    }];
    [self layoutIfNeeded];
    [self setNeedsLayout];
    
    [self performSelector:@selector(HOTCHATmoveAction:) withObject:@"1" afterDelay:0.01];
}
- (void)HOTCHATmoveAction:(NSString *)isOpen{
    if ([isOpen isEqualToString:@"1"]) {
        self.isShow = YES;
        self.HOTCHATtopview.backgroundColor = [UIColor clearColor];
    }else{
        self.isShow = NO;
    }
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.isShow) {
            self.HOTCHATtopview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        }else{
            self.HOTCHATtopview.backgroundColor = [UIColor clearColor];
        }
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            if (!self.isShow) {
                [self removeFromSuperview];
            }
        }
    }];
}

- (void)updateConstraints{
    [super updateConstraints];
    
    CGFloat height = CGRectGetHeight(self.frame);
    CGFloat pheight = (self.isShow ? self.dataArray.count * 95 : 0);
    if (pheight > (height - 150)) {
        pheight = height - 150;
    }
    if (pheight < 0){
        pheight = 0;
    }
    
    [self.HOTCHATroundview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(pheight);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (CGRectContainsPoint(self.HOTCHATphotoListTableview.frame, point)) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHAThiddenListView)]) {
        [self.delegate HOTCHAThiddenListView];
    }
}
- (void)HOTCHATcloseListView{
    //    [self removeFromSuperview];
    [self HOTCHATmoveAction:@"0"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATcloseListView)]) {
        [self.delegate HOTCHATcloseListView];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //    if (!CGRectEqualToRect(CGRectZero, self.HOTCHATphotoListTableview.frame)) {
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.HOTCHATroundview.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(6, 6)];
    
    CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
    
    maskLayer.frame = self.HOTCHATroundview.bounds;
    
    maskLayer.path = maskPath.CGPath;
    
    self.HOTCHATroundview.layer.mask = maskLayer;
    //    self.HOTCHATroundview.layer.masksToBounds = YES;
    //    }
}

- (UITableView *)HOTCHATphotoListTableview{
    if (_HOTCHATphotoListTableview == nil) {
        _HOTCHATphotoListTableview = [[UITableView alloc] init];
        _HOTCHATphotoListTableview.dataSource = self;
        _HOTCHATphotoListTableview.delegate = self;
        _HOTCHATphotoListTableview.rowHeight = 95;
        _HOTCHATphotoListTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_HOTCHATphotoListTableview registerClass:[PhotoListCellTableViewCell class] forCellReuseIdentifier:@"PhotoListCellTableViewCell"];
        //        _HOTCHATphotoListTableview.layer.masksToBounds = YES;
    }
    return _HOTCHATphotoListTableview;
}
- (UIView *)HOTCHATtopview{
    if (_HOTCHATtopview == nil) {
        _HOTCHATtopview = [[UIView alloc] init];
        _HOTCHATtopview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return _HOTCHATtopview;
}
- (UIView *)HOTCHATbottomview{
    if (_HOTCHATbottomview == nil) {
        _HOTCHATbottomview = [[UIView alloc] init];
    }
    return _HOTCHATbottomview;
}
- (UIView *)HOTCHATroundview{
    if (_HOTCHATroundview == nil) {
        _HOTCHATroundview = [[UIView alloc] init];
        _HOTCHATroundview.backgroundColor = [UIColor whiteColor];
        _HOTCHATroundview.clipsToBounds = YES;
    }
    return _HOTCHATroundview;
}
@end
